package config

import (
	"gitee.com/liuziloong/ga1400gateway/tools"
	"gitee.com/liuziloong/ga1400gateway/tools/logs"
	"github.com/astaxie/beego"
	"os"
	"strings"
)

var (
	ServiceName      string   //服务名称
	HttpAddressList  []string //HTTP服务端地址
	HttpOvertime     int64    //http转发图片超时时间
	VIIDServerID     string   //路由/VIID/System/Time使用
	ImageFacesPath   string   //人脸新增图片存放路径
	ImagePersonsPath string   //人员新增图片存放路径
)

func init() {
	ServiceName = beego.AppConfig.String("serviceName")
	if ServiceName == "" {
		ServiceName = "GW1400"
		logs.BeeLogger.Error("init serviceName error, serviceName cannot be empty, set default value: serviceName=%s", ServiceName)
	}

	HttpAddressList = beego.AppConfig.Strings("httpAddressList")
	switch len(HttpAddressList) {
	case 0:
		logs.PanicLogger.Panicln("init httpAddressList error, httpAddressList cannot be empty, please check it")
	default:
		for _, v := range HttpAddressList {
			if v == "" {
				logs.PanicLogger.Panicln("init httpAddressList error, httpAddress cannot be empty, please check it")
			}
		}
	}

	//for _, v := range HttpAddressList {
	//	fmt.Println("打印地址：", v)
	//}

	var err error
	HttpOvertime, err = beego.AppConfig.Int64("httpOvertime")
	if err != nil || HttpOvertime <= 0 {
		HttpOvertime = 3
		logs.BeeLogger.Error("init httpOvertime error, httpOvertime should be a positive integer, set default value: httpOvertime=%d", HttpOvertime)
	}

	VIIDServerID = beego.AppConfig.String("VIIDServerID")
	if VIIDServerID == "" {
		VIIDServerID = tools.GetUUID()
		logs.BeeLogger.Error("init VIIDServerID error, VIIDServerID cannot be empty, set VIIDServerID=%s", VIIDServerID)
	}

	ImageFacesPath = beego.AppConfig.String("imageFacesPath")
	if ImageFacesPath == "" {
		ImageFacesPath = "./image/faces"
		logs.BeeLogger.Error("init imageFacesPath error, imageFacesPath cannot be empty, set imageFacesPath=%s", ImageFacesPath)
	}
	//判断路径是否以./开头
	if strings.HasPrefix(ImageFacesPath, "./") {
		//以./开头使用当前程序执行路径
		ImageFacesPath = tools.StringsJoin(tools.GetAbsPath(), ImageFacesPath[2:])
	}

	err = os.MkdirAll(ImageFacesPath, os.ModePerm)
	if err != nil {
		logs.PanicLogger.Panicln("init config os.MkdirAll() error:", err)
	}

	ImagePersonsPath = beego.AppConfig.String("imagePersonsPath")
	if ImagePersonsPath == "" {
		ImagePersonsPath = "./image/persons"
		logs.BeeLogger.Error("init imagePersonsPath error, imagePersonsPath cannot be empty, set imagePersonsPath=%s", ImagePersonsPath)
	}
	//判断路径是否以./开头
	if strings.HasPrefix(ImagePersonsPath, "./") {
		//以./开头使用当前程序执行路径
		ImagePersonsPath = tools.StringsJoin(tools.GetAbsPath(), ImagePersonsPath[2:])
	}

	err = os.MkdirAll(ImagePersonsPath, os.ModePerm)
	if err != nil {
		logs.PanicLogger.Panicln("init config os.MkdirAll() error:", err)
	}
}
